home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Headers / STL Headers / defalloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-24  |  4.6 KB  |  164 lines  |  [TEXT/MMCC]

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef DEFALLOC_H
  17. #define DEFALLOC_H
  18.  
  19. #include <new.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. //#include <iostream.h>
  24. #include <algobase.h>
  25.  
  26. //inline void* operator new(size_t, void* p) {return p;}
  27.  
  28. template <class T>
  29. inline T* allocate(ptrdiff_t size, T*) {
  30.     return (T*)(::operator new((size_t)(size * sizeof(T)))); // Let 'new' throw an exception
  31.                                                              // if necessary
  32. }
  33.  
  34. /*
  35.  * the following template function is replaced by the following two functions
  36.  * due to the fact that the Borland compiler doesn't change ptrdiff_t type
  37.  * to type long when compile with -ml or -mh.
  38.  *
  39. template <class T>
  40. inline T* allocate(ptrdiff_t size, T*) {
  41.     fvoid_t* save_new_handler = set_new_handler(0);
  42.     T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
  43.     if (tmp == 0) {
  44.     cerr << "out of memory" << endl; 
  45.     exit(1);
  46.     }
  47.     set_new_handler(save_new_handler);
  48.     return tmp;
  49. }
  50. /*
  51. template <class T>
  52. inline T* allocate(int size, T*) {
  53.     set_new_handler(0);
  54.     T* tmp = (T*)(::operator new((unsigned int)(size * sizeof(T))));
  55.     if (tmp == 0) {
  56.     cerr << "out of memory" << endl; 
  57.     exit(1);
  58.     }
  59.     return tmp;
  60. }
  61.  
  62. template <class T>
  63. inline T* allocate(long size, T*) {
  64.     set_new_handler(0);
  65.     T* tmp = (T*)(::operator new((unsigned long)(size * sizeof(T))));
  66.     if (tmp == 0) {
  67.     cerr << "out of memory" << endl; 
  68.     exit(1);
  69.     }
  70.     return tmp;
  71. }
  72. */
  73. template <class T>
  74. inline void deallocate(T* buffer) {
  75.     ::operator delete(buffer);
  76. }
  77.  
  78. template <class T>
  79. inline void destroy(T* pointer) {
  80.     pointer->~T();
  81. }
  82.  
  83. inline void destroy(char*) {}
  84. inline void destroy(unsigned char*) {}
  85. inline void destroy(short*) {}
  86. inline void destroy(unsigned short*) {}
  87. inline void destroy(int*) {}
  88. inline void destroy(unsigned int*) {}
  89. inline void destroy(long*) {}
  90. inline void destroy(unsigned long*) {}
  91. inline void destroy(float*) {}
  92. inline void destroy(double*) {}
  93. inline void destroy(char**) {}
  94. inline void destroy(unsigned char**) {}
  95. inline void destroy(short**) {}
  96. inline void destroy(unsigned short**) {}
  97. inline void destroy(int**) {}
  98. inline void destroy(unsigned int**) {}
  99. inline void destroy(long**) {}
  100. inline void destroy(unsigned long**) {}
  101. inline void destroy(float**) {}
  102. inline void destroy(double**) {}
  103.  
  104. inline void destroy(char*, char*) {}
  105. inline void destroy(unsigned char*, unsigned char*) {}
  106. inline void destroy(short*, short*) {}
  107. inline void destroy(unsigned short*, unsigned short*) {}
  108. inline void destroy(int*, int*) {}
  109. inline void destroy(unsigned int*, unsigned int*) {}
  110. inline void destroy(long*, long*) {}
  111. inline void destroy(unsigned long*, unsigned long*) {}
  112. inline void destroy(float*, float*) {}
  113. inline void destroy(double*, double*) {}
  114. inline void destroy(char**, char**) {}
  115. inline void destroy(unsigned char**, unsigned char**) {}
  116. inline void destroy(short**, short**) {}
  117. inline void destroy(unsigned short**, unsigned short**) {}
  118. inline void destroy(int**, int**) {}
  119. inline void destroy(unsigned int**, unsigned int**) {}
  120. inline void destroy(long**, long**) {}
  121. inline void destroy(unsigned long**, unsigned long**) {}
  122. inline void destroy(float**, float**) {}
  123. inline void destroy(double**, double**) {}
  124.  
  125. template <class T1, class T2>
  126. inline void construct(T1* p, const T2& value) {
  127.     new (p) T1(value);
  128. }
  129.  
  130. template <class T>
  131. class allocator {
  132. public:
  133.     typedef T value_type;
  134.     typedef T* pointer;
  135.     typedef const T* const_pointer;
  136.     typedef T& reference;
  137.     typedef const T& const_reference;
  138.     typedef size_t size_type;
  139.     typedef ptrdiff_t difference_type;
  140.     pointer allocate(size_type n) { 
  141.     return ::allocate((difference_type)n, (pointer)0);
  142.     }
  143.     void deallocate(pointer p) { ::deallocate(p); }
  144.     pointer address(reference x) { return (pointer)&x; }
  145.     const_pointer const_address(const_reference x) { 
  146.     return (const_pointer)&x; 
  147.     }
  148.     size_type init_page_size() { 
  149.     return max(size_type(1), size_type(4096/sizeof(T))); 
  150.     }
  151.     size_type max_size() const { 
  152.     return max(size_type(1), size_type(ULONG_MAX/sizeof(T))); 
  153.     }
  154. };
  155.  
  156. class allocator<void> {
  157. public:
  158.     typedef void* pointer;
  159. };
  160.  
  161.  
  162.  
  163. #endif
  164.